<template>
  <view class="main_box">
    <view class="carousel">
      <image src="http://129.211.211.126/download/mjkf.png" />
    </view>
    <view class="main_icon_box">
      <icon-label
        v-for="(item, index) in icons"
        style="margin-bottom: 20rpx"
        :style="{
          marginRight: (index + 1) % 5 === 0 ? '0' : '52rpx',
        }"
        :key="index"
        :item="item"
        @tap="updatePassword(item)"
      />
    </view>
    <view class="msg_box">
      <image class="msg_icon" src="/static/image/msg.png" />
      <swiper circular="true" vertical="true" autoplay="true" interval="3000" duration="1000" class="swiperBox">
        <swiper-item v-for="(item, index) in noticeList" :key="index">
          <view @click="selectNotice(item)" class="notice-content">
            <view class="fl1"><u-parse :content="item.content"></u-parse></view>
            <view class="dot" v-if="item.isRead === 0"></view>
          </view>
        </swiper-item>
      </swiper>

      <image class="right_icon" src="/static/image/right.png" />
    </view>

    <image class="xcpz" src="http://129.211.211.126/download/xcpz.png" @click="goAddWorkLog" />
    <view style="width: 95%; justify-content: space-between" class="flex a-item" @click="goWorkLogList">
      <view class="gzrz_text"> 工作日志列表 </view>
      <view class="flex gd">更多<image src="/static/image/right.png" /> </view>
    </view>
    <work-log class="work_log" :data="workLogData" />
    <u-popup :show="showPop" mode="center" @close="close" round="5" :safeAreaInsetBottom="false">
      <view class="pop-content">
        <view class="flex j-content-between">
          <view class="tip">公告</view>
          <u-icon size="20" name="close" @click="close"></u-icon>
        </view>
        <view class="title">{{ noticeItem.name }}</view>
        <view class="content">
          <u-parse :content="noticeItem.content"></u-parse>
        </view>
        <view class="pop-content-bottom"> <button class="submit-btn" size="mini" @click="readNotice">确定</button></view>
      </view>
    </u-popup>
  </view>
</template>

<script>
import IconLabel from '@/components/iconLabel.vue'
import { getRecentUsed } from '@/service/auth'
import WorkLog from '@/components/workLog.vue'
import { getMyNotice, readMyNotice, workLogList } from '@/service/api/common_down'
export default {
  name: 'home',
  components: { WorkLog, IconLabel },
  data() {
    return {
      icons: [
        {
          name: '打卡',
          icon: '../../../static/image/sign_in.png',
          url: '../clock/index',
        },
        {
          name: '补卡',
          icon: '../../../static/image/buka.png',
          url: '../clock/index',
        },
        {
          name: '请假',
          icon: '../../../static/image/qj.png',
          url: '../clock/index',
        },
        {
          name: '采购申请',
          icon: '../../../static/image/cgsq.png',
          url: '../clock/index',
        },
        {
          name: '销售订单',
          icon: '../../../static/image/xsdd.png',
          url: '../clock/index',
        },
        {
          name: '库存记录',
          icon: '../../../static/image/kcjl.png',
          url: '../clock/index',
        },
        {
          name: '更多',
          icon: '../../../static/image/gd.png',
          url: '../clock/index',
        },
      ],
      size: 999,
      page: 1,
      noticeList: [],
      noticeItem: {},
      showPop: false,
      workLogData: {},
    }
  },
  
  onShow() {
    this.$forceUpdate()
    uni.$on('refreshData', res => {
      if (res.refresh) {
        //更新数据
        this.getWorkLogList()
      }
    })
  },
  onPullDownRefresh() {},
  created() {
    this.icons = getRecentUsed()
  },
  mounted() {
    this.getNoticeList()
    this.getWorkLogList()
  },
  methods: {
    readNotice() {
      if (this.noticeItem.isRead === 1) {
        this.close()
        return
      }
      this.uniShowLoading()
      readMyNotice({ ids: this.noticeItem.id })
        .then(res => {
          this.uniHideLoading()
          this.close()
          this.getNoticeList()
        })
        .catch(err => {
          this.uniHideLoading()
          // uni.showToast({
          //   title: err.msg,
          //   icon: 'none',
          // })
        })
    },

    close() {
      this.showPop = false
    },
    selectNotice(item) {
      this.noticeItem = item
      this.showPop = true
    },
    getNoticeList() {
      const data = {
        size: this.size,
        page: this.page,
      }
      this.uniShowLoading()
      getMyNotice(data)
        .then(res => {
          this.uniHideLoading()
          this.noticeList = res.data.content
        })
        .catch(err => {
          this.uniHideLoading()
          // uni.showToast({
          //   title: err.msg,
          //   icon: 'none',
          // })
        })
    },
    goAddWorkLog() {
      uni.navigateTo({
        url: `/pageAttendance/pages/workLog/addWorkLog`,
      })
    },
    goWorkLogList() {
      uni.navigateTo({
        url: `/pageAttendance/pages/workLog/list`,
      })
    },
    getWorkLogList() {
      const _this = this
      const data = {
        size: '1',
        page: this.page,
        sort:'id desc'
      }
      this.uniShowLoading()
      workLogList(data)
        .then(res => {
          this.uniHideLoading()
          console.log(res)
          if (res.data.content.length > 0) {
            _this.workLogData = res.data.content[0]
          }
        })
        .catch(err => {
          this.uniHideLoading()
        })
    },
    updatePassword(obj) {
      console.log(obj)
     
      uni.navigateTo({ url: obj.url })
    },
  },
}
</script>

<style lang="scss" scoped>
.main_box {
  width: 100vw;
  min-height: 100vh;
  display: flex;
  overflow-y: auto;
  flex-direction: column;
  padding-top: 20%;
  align-items: center;

  background-image: url('http://129.211.211.126/download/homeBg.png');
  background-size: cover;
  background-repeat: repeat;
  .carousel {
    width: 95%;
    image {
      width: 100%;
      height: 300rpx;
    }
  }
  .main_icon_box {
    width: 95%;
    display: flex;
    flex-wrap: wrap;
    margin-top: 30rpx;
  }
  .msg_box {
    background: white;
    width: 95%;
    height: 65rpx;
    border-radius: 10rpx;
    display: flex;
    align-items: center;
    .msg_icon {
      width: 40rpx;
      height: 40rpx;
      margin: 0 30rpx;
    }
    .right_icon {
      width: 15rpx;
      height: 30rpx;
      margin: 0 30rpx;
    }
    .msg_container {
      border-left: rgba(136, 136, 136, 0.5) 3rpx solid;
      height: 40rpx;
      overflow: hidden;
      padding-left: 30rpx;
      font-size: 26rpx;
      color: rgb(136, 136, 136);
      flex: 1;
      white-space: nowrap;
      .message {
        white-space: nowrap;
        overflow: hidden;
        width: 100%;
        height: 40rpx;
        line-height: 40rpx;

        /* animation: scroll 2s linear infinite; */
      }
    }

    .swiperBox {
      width: 500rpx;
      height: 40rpx;
      font-size: 26rpx;
      color: rgb(136, 136, 136);
      .notice-content {
        display: flex;
        align-items: center;
        .dot {
          width: 10rpx;
          height: 10rpx;
          border-radius: 50%;
          background-color: red;
        }
      }
    }
  }
  .xcpz {
    width: 95%;
    height: 250rpx;
    margin-top: 30rpx;
  }
  .gzrz_text {
    font-size: 38rpx;
    font-weight: bold;
    margin-top: 20rpx;
    border-bottom: transparent 10rpx solid;
    border-image: linear-gradient(to right, rgba(47, 111, 239, 0), rgb(47, 111, 239)) 1;
  }
  .gd {
    color: rgb(136, 136, 136);
    font-size: 28rpx;
    width: 100rpx;
    align-items: center;
    image {
      width: 15rpx;
      height: 30rpx;
      margin-left: 20rpx;
    }
  }
}
.work_log {
  background: white;
  width: 95%;
  margin: 20rpx 0;
  border-radius: 10rpx;
}
.pop-content {
  width: 500rpx;
  padding: 20rpx;
  .tip {
    font-size: 26rpx;
  }
  .title {
    font-size: 32rpx;
    font-weight: bold;
    text-align: center;
  }
  .content {
    text-align: center;
    margin: 30rpx 0;
  }
  .pop-content-bottom {
    display: flex;
    justify-content: center;
  }
  .submit-btn {
    width: 200rpx;
    height: 60rpx;
    background-color: #2f6fef;
    border-radius: 30rpx;
    color: white;
  }
}
</style>
